Skip to content

fix(crs-compose): ignore symlinks and hidden files in artifact inputs and forwarding - #530

Open
imshubham22apr-gif wants to merge 2 commits into
ossf:mainfrom
imshubham22apr-gif:fix/copy-artifact-input-symlink-guard
Open

imshubham22apr-gif wants to merge 2 commits into
ossf:mainfrom
imshubham22apr-gif:fix/copy-artifact-input-symlink-guard

Conversation

@imshubham22apr-gif

Copy link
Copy Markdown
Contributor

Summary

Hardens artifact input copying and forward artifact copying in CRSCompose against copying symlinks, out-of-tree links, or hidden bookkeeping files into the exchange directory.

Changes

  • In oss_crs/src/crs_compose.py:
    • In _copy_forward_artifact_sources(): passed ignore=shutil.ignore_patterns(".*") and symlinks=False to shutil.copytree().
    • In _copy_artifact_input(): passed ignore=shutil.ignore_patterns(".*") and symlinks=False to shutil.copytree() for recursive directories; filtered out symlinks and hidden files (not f.is_symlink() and not f.name.startswith(".")) when copying flat directories.
  • Added unit tests in oss_crs/tests/unit/test_directed_inputs_behavior.py:
    • Verified that hidden files and symlinks are excluded when forwarding artifacts into the exchange directory.
  • Updated CHANGELOG.md.

Verification

  • uv run pytest oss_crs/tests/unit/test_directed_inputs_behavior.py -k "test_copy_forward_artifact_sources" passed.
  • uv run ruff check oss_crs/src/crs_compose.py oss_crs/tests/unit/test_directed_inputs_behavior.py passed.
  • uv run ruff format --check oss_crs/src/crs_compose.py oss_crs/tests/unit/test_directed_inputs_behavior.py passed.
  • uv run pyright oss_crs/src/crs_compose.py oss_crs/tests/unit/test_directed_inputs_behavior.py passed (0 errors, 0 warnings).

… and forwarding

Signed-off-by: imshubham22apr-gif <imshubham.22apr@gmail.com>

@vikramsingh920 vikramsingh920 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on hardening artifact copying!

I tested this locally and checked the implementation, and noticed an important edge case regarding how shutil.copytree handles symlinks=False:

1. symlinks=False in shutil.copytree follows and copies the target file

In Python's standard library shutil.copytree:

  • symlinks=True: Copies the symlink object itself.
  • symlinks=False (the default): Does not ignore symlinks; instead, it follows the symlink and copies the contents of the target file into the destination tree.

Because shutil.ignore_patterns(".*") only checks filename strings matching .*, symlinks pointing to outside files are still followed and copied as regular files.

Suggested Fix:

To properly ignore both hidden files and symlinks during recursive copying, a custom ignore callable can be used:

def _ignore_hidden_and_symlinks(directory: str, contents: list[str]) -> set[str]:
    ignored = set()
    for name in contents:
        path = os.path.join(directory, name)
        if name.startswith(".") or os.path.islink(path):
            ignored.add(name)
    return ignored

And pass ignore=_ignore_hidden_and_symlinks to shutil.copytree().


2. Unit Test Symlink Creation False-Positive on Windows

In test_copy_forward_artifact_sources_skips_hidden_and_symlinks:

    try:
        symlink_file.symlink_to(outside)
    except OSError:
        pass

On environments where unprivileged symlink creation raises OSError (e.g. non-Developer mode Windows), link.diff is silently never created in the source directory. This causes assert not (dst / "patches" / "link.diff").exists() to pass falsely because the file never existed in the first place.

Suggested Fix:

If symlink_to raises OSError, consider skipping the test so it doesn't give a false positive:

    try:
        symlink_file.symlink_to(outside)
    except OSError:
        pytest.skip("Symlinks cannot be created without elevated privileges on this platform")

@imshubham22apr-gif

Copy link
Copy Markdown
Contributor Author

Thanks @vikramsingh920 for catching both of these! Addressed in 15aebf9:

  1. Symlink exclusion in shutil.copytree(): Added CRSCompose._ignore_hidden_and_symlinks(directory, names) (checking name.startswith(".") or os.path.islink(os.path.join(directory, name))) and passed it as ignore= to both shutil.copytree() calls in _copy_forward_artifact_sources() and _copy_artifact_input(), so symlinks to files or directories are excluded before copytree() can follow them.
  2. Symlink test false-positive: Updated the regression tests for both paths in oss_crs/tests/unit/test_directed_inputs_behavior.py to call pytest.skip(...) if symlink_to() raises OSError, and to assert is_symlink() after creation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants